home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / FileSystemView.java < prev    next >
Text File  |  1998-06-30  |  11KB  |  416 lines

  1. /*
  2.  * @(#)FileSystemView.java    1.4 98/04/14
  3.  * 
  4.  * Copyright (c) 1997,1998 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.preview.filechooser;
  22.  
  23. import com.sun.java.swing.preview.*;
  24. import com.sun.java.swing.event.*;
  25. import com.sun.java.swing.*;
  26.  
  27. import java.io.File;
  28. import java.io.IOException;
  29. import java.util.Vector;
  30.  
  31. /**
  32.  * FileSystemView is JFileChooser's gateway to the
  33.  * file system. Since the JDK1.1 File api doesn't allow
  34.  * access to such information as root partitians, file type
  35.  * information, or hidden file bits, this class is designed
  36.  * to intuit as much OS specific file system information as
  37.  * possible.
  38.  *
  39.  * FileSystemView will eventually delegate its responsibilities
  40.  * to io File classes when JDK1.X provides more direct access to
  41.  * file system information.
  42.  *
  43.  * This implementation of FileSystemView tries to determine
  44.  * if the file system is a Windows box. If it isn't, it
  45.  * assumes that this is a Unix box.
  46.  *
  47.  * Java Licenses may want to provide a different implemenation of
  48.  * FileSystemView to better handle a given operation system.
  49.  *
  50.  * PENDING(jeff) - need to provide a specification for
  51.  * how Mac/OS2/BeOS/etc file systems can modify FileSystemView
  52.  * to handle their particular type of file system.
  53.  *
  54.  * @version 1.4 04/14/98
  55.  * @author Jeff Dinkins
  56.  */
  57. public abstract class FileSystemView {
  58.  
  59.     static FileSystemView windowsFileSystemView = null;
  60.     static FileSystemView unixFileSystemView = null;
  61.     //static FileSystemView macFileSystemView = null;
  62.     static FileSystemView genericFileSystemView = null;
  63.  
  64.     public static FileSystemView getFileSystemView() {
  65.     if(File.separatorChar == '\\') {
  66.         if(windowsFileSystemView == null) {
  67.         windowsFileSystemView = new WindowsFileSystemView();
  68.         }
  69.         return windowsFileSystemView;
  70.     }
  71.  
  72.     if(File.separatorChar == '/') {
  73.         if(unixFileSystemView == null) {
  74.         unixFileSystemView = new UnixFileSystemView();
  75.         }
  76.         return unixFileSystemView;
  77.     }
  78.  
  79.     // if(File.separatorChar == ':') {
  80.     //    if(macFileSystemView == null) {
  81.     //    macFileSystemView = new MacFileSystemView();
  82.     //    }
  83.     //    return macFileSystemView;
  84.     //}
  85.  
  86.     if(genericFileSystemView == null) {
  87.         genericFileSystemView = new GenericFileSystemView();
  88.     }
  89.     return genericFileSystemView;
  90.     }
  91.  
  92.     /**
  93.      * Determines if the given file is a root partition or drive.
  94.      */
  95.     public abstract boolean isRoot(File f);
  96.  
  97.     /**
  98.      * creates a new folder with a default folder name.
  99.      */
  100.     public abstract File createNewFolder(File containingDir) throws IOException;
  101.  
  102.     /**
  103.      * Returns whether a file is hidden or not. 
  104.      */
  105.     public abstract boolean isHiddenFile(File f);
  106.  
  107.  
  108.     /**
  109.      * Returns all root partitians on this system. For example, on Windows,
  110.      * this would be the A: through Z: drives.
  111.      */
  112.     public abstract File[] getRoots();
  113.  
  114.  
  115.     // Providing default implemenations for the remaining methods
  116.     // because most OS file systems will likely be able to use this
  117.     // code. If a given OS can't, override these methods in its
  118.     // implementation.
  119.  
  120.     /**
  121.      * Returns a File object constructed in dir from the given filename.
  122.      */
  123.     public File createFileObject(File dir, String filename) {
  124.     if(dir == null) {
  125.         return new File(filename);
  126.     } else {
  127.         return new File(dir, filename);
  128.     }
  129.     }
  130.  
  131.     /**
  132.      * Returns a File object constructed from the given path string.
  133.      */
  134.     public File createFileObject(String path) {
  135.     return new File(path);
  136.     }
  137.  
  138.  
  139.     /**
  140.      * gets the list of shown (i.e. not hidden) files
  141.      */
  142.     public File[] getFiles(File dir, boolean useFileHiding) {
  143.     Vector files = new Vector();
  144.  
  145.     // add all files in dir
  146.     String[] names = dir.list();
  147.     
  148.     File f;
  149.  
  150.     int nameCount = names == null ? 0 : names.length;
  151.  
  152.     for (int i = 0; i < nameCount; i++) {
  153.         f = createFileObject(dir, names[i]);
  154.         if(useFileHiding) {
  155.         if(!isHiddenFile(f)) {
  156.             files.addElement(f);
  157.         } 
  158.         } else {
  159.         files.addElement(f);
  160.         }
  161.     }
  162.  
  163.     File[] fileArray = new File[files.size()];
  164.     files.copyInto(fileArray);
  165.     
  166.     return fileArray;
  167.     }
  168.     
  169.     /**
  170.      * Returns the parent directory of dir. 
  171.      */
  172.     public File getParentDirectory(File dir) {
  173.     if(dir != null) {
  174.         File f = createFileObject(dir.getAbsolutePath());
  175.         String parentFilename = f.getParent();
  176.         if(parentFilename != null) {
  177.         return new File(parentFilename);
  178.         }
  179.     }
  180.     return null;
  181.     }
  182. }
  183.  
  184. /**
  185.  * FileSystemView that handles some specific unix-isms.
  186.  */
  187. class UnixFileSystemView extends FileSystemView {
  188.  
  189.     public boolean isRoot(File f) {
  190.     String path = f.getAbsolutePath();
  191.     if(path.length() == 1 && path.charAt(0) == '/') {
  192.         return true;
  193.     } 
  194.     return false;
  195.     }
  196.  
  197.     /**
  198.      * creates a new folder with a default folder name.
  199.      */
  200.     public File createNewFolder(File containingDir) throws IOException {
  201.     if(containingDir == null) {
  202.         throw new IOException("Containing directory is null:");
  203.     }
  204.     File newFolder = null;
  205.     // Unix - using OpenWindow's default folder name. Can't find one for Motif/CDE.
  206.     newFolder = createFileObject(containingDir, "NewFolder");
  207.     int i = 1;
  208.     while (newFolder.exists() && (i < 100)) {
  209.         newFolder = createFileObject(containingDir, "NewFolder." + i);
  210.         i++;
  211.     }
  212.  
  213.     if(newFolder.exists()) {
  214.         throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
  215.     } else {
  216.         newFolder.mkdirs();
  217.     }
  218.  
  219.     return newFolder;
  220.     }
  221.  
  222.     /**
  223.      * Returns whether a file is hidden or not. On Unix, 
  224.      * all files that begin with "." are hidden. 
  225.      */
  226.     public boolean isHiddenFile(File f) {
  227.     if(f != null) {
  228.         String filename = f.getName();
  229.         if(filename.charAt(0) == '.') {
  230.         return true;
  231.         } else {
  232.         return false;
  233.         }
  234.     } 
  235.     return false;
  236.     }
  237.  
  238.     /**
  239.      * Returns the root partitian on this system. On Unix, this is just "/".
  240.      */
  241.     public File[] getRoots() {
  242.     File[] roots = new File[1];
  243.     roots[0] = new File("/");
  244.     if(roots[0].exists() && roots[0].isDirectory()) {
  245.         return roots;
  246.     }
  247.     return null;
  248.     }
  249.  
  250. }
  251.  
  252.  
  253. /**
  254.  * FileSystemView that handles some specific windows concepts.
  255.  */
  256. class WindowsFileSystemView extends FileSystemView {
  257.  
  258.     /**
  259.      * Returns true if the given file is a root.
  260.      */
  261.     public boolean isRoot(File f) {
  262.     if(!f.isAbsolute()) {
  263.         return false;
  264.     }
  265.     
  266.         String parentPath = f.getParent();
  267.         if(parentPath == null) {
  268.             return true;
  269.         } else {
  270.             File parent = new File(parentPath);
  271.             return parent.equals(f);
  272.         }
  273.     }
  274.  
  275.     /**
  276.      * creates a new folder with a default folder name.
  277.      */
  278.     public File createNewFolder(File containingDir) throws IOException {
  279.     if(containingDir == null) {
  280.         throw new IOException("Containing directory is null:");
  281.     }
  282.     File newFolder = null;
  283.     // Using NT's default folder name
  284.     newFolder = createFileObject(containingDir, "New Folder");
  285.     int i = 2;
  286.     while (newFolder.exists() && (i < 100)) {
  287.         newFolder = createFileObject(containingDir, "New Folder (" + i + ")");
  288.         i++;
  289.     }
  290.  
  291.     if(newFolder.exists()) {
  292.         throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
  293.     } else {
  294.         newFolder.mkdirs();
  295.     }
  296.  
  297.     return newFolder;
  298.     }
  299.  
  300.     /**
  301.      * Returns whether a file is hidden or not. On Windows 
  302.      * there is currently no way to get this information from
  303.      * io.File, therefore always return false.
  304.      */
  305.     public boolean isHiddenFile(File f) {
  306.     return false;
  307.     }
  308.  
  309.     /**
  310.      * Returns all root partitians on this system. On Windows, this
  311.      * will be the A: through Z: drives.
  312.      */
  313.     public File[] getRoots() {
  314.     Vector rootsVector = new Vector();
  315.     
  316.     // Create the A: drive whether it is mounted or not
  317.     WindowsFloppy floppy = new WindowsFloppy();
  318.     rootsVector.addElement(floppy);
  319.     
  320.     // Run through all possible mount points and check
  321.     // for their existance.
  322.     for (char c = 'C'; c <= 'Z'; c++) {
  323.         char device[] = {c, ':', '\\'};
  324.         String deviceName = new String(device);
  325.         File deviceFile = new File(deviceName);
  326.         if (deviceFile != null && deviceFile.exists()) {
  327.         rootsVector.addElement(deviceFile);
  328.         }
  329.     }
  330.     File[] roots = new File[rootsVector.size()];
  331.     rootsVector.copyInto(roots);
  332.     return roots;
  333.     }
  334.  
  335.     /**
  336.      * Fake the floppy drive. There is no way to know whether
  337.      * it is mounted or not, and doing a file.isDirectory or
  338.      * file.exists() causes Windows to pop up the "Insert Floppy"
  339.      * dialog. We therefore assume that A: is the floppy drive,
  340.      * and force it to always return true for isDirectory()
  341.      */
  342.     class WindowsFloppy extends File {
  343.     public WindowsFloppy() {
  344.         super("A" + ":" + "\\");
  345.     }
  346.  
  347.     public boolean isDirectory() {
  348.         return true;
  349.     };
  350.     }
  351.  
  352. }
  353.  
  354.  
  355. /**
  356.  * Fallthrough FileSystemView in case we can't determine the OS.
  357.  */
  358. class GenericFileSystemView extends FileSystemView {
  359.  
  360.     /**
  361.      * Returns true if the given file is a root.
  362.      */
  363.     public boolean isRoot(File f) {
  364.     if(!f.isAbsolute()) {
  365.         return false;
  366.     }
  367.     
  368.         String parentPath = f.getParent();
  369.         if(parentPath == null) {
  370.             return true;
  371.         } else {
  372.             File parent = new File(parentPath);
  373.             return parent.equals(f);
  374.         }
  375.     }
  376.  
  377.     /**
  378.      * creates a new folder with a default folder name.
  379.      */
  380.     public File createNewFolder(File containingDir) throws IOException {
  381.     if(containingDir == null) {
  382.         throw new IOException("Containing directory is null:");
  383.     }
  384.     File newFolder = null;
  385.     // Using NT's default folder name
  386.     newFolder = createFileObject(containingDir, "NewFolder");
  387.  
  388.     if(newFolder.exists()) {
  389.         throw new IOException("Directory already exists:" + newFolder.getAbsolutePath());
  390.     } else {
  391.         newFolder.mkdirs();
  392.     }
  393.  
  394.     return newFolder;
  395.     }
  396.  
  397.     /**
  398.      * Returns whether a file is hidden or not. Since we don't
  399.      * know the OS type, always return false
  400.      */
  401.     public boolean isHiddenFile(File f) {
  402.     return false;
  403.     }
  404.  
  405.     /**
  406.      * Returns all root partitians on this system. Since we
  407.      * don't know what OS type this is, return a null file
  408.      * list.
  409.      */
  410.     public File[] getRoots() {
  411.     File[] roots = new File[0];
  412.     return roots;
  413.     }
  414.  
  415. }
  416.